package com.example.a79273.nvshenyichu.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a79273.nvshenyichu.R;
import com.example.a79273.nvshenyichu.adapter.MatchAdapter;
import com.example.a79273.nvshenyichu.config.MatchNetconfig;
import com.example.a79273.nvshenyichu.model.MatchTitleBean;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 79273 on 2016/8/28.
 */
public class DaPei extends Fragment{
    private View view;
    private View mViewTitle;
    private TextView mTvTitle;
    private TabLayout mTabLayout;
    private ViewPager mViewPager;
    private RequestParams mParams;
    private List<String> mListTitle;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mData;
    private Fragment mFragment;
    private List<MatchTitleBean> mList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_match,null);
        initView();
        initData();
        setData();
        setLisetener();

        return view;
    }

    private void setLisetener() {

    }

    private void setData() {
    mViewPager.setAdapter(mAdapter);

    }

    private void initData() {
        mList = new ArrayList<>();
        mListTitle = new ArrayList<>();
        mData = new ArrayList<>();
        mAdapter = new MatchAdapter(getActivity().getSupportFragmentManager(),mData,mListTitle);
        //-----------------------请求数据---------------------
        mParams = new RequestParams(MatchNetconfig.TITLE_PATH);
        x.http().get(mParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if(!TextUtils.isEmpty(result)) {
                    Gson gson = new Gson();
                    mList.addAll(gson.fromJson(result, new ArrayList<MatchTitleBean>().getClass()));

                    Log.d("TAG","----list解析出来的大小"+mList.size());
                    for (int i = 0; i < mList.size(); i++) {
                        MatchTitleBean matchTitleBean = mList.get(i);
                        String title = matchTitleBean.getTitle();
                        Log.i("TAG","----标题"+title);
                        mListTitle.add(title);
                    }
                    Log.d("TAG","----标题集合的大小"+mListTitle.size());
                    for (int j = 0; j < mListTitle.size(); j++) {
                        mFragment = new FragmentMatchSon();
                        mData.add(mFragment);
                    }
                    mAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(getActivity(), "网络出小差了...", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    private void initView() {
        mViewTitle = view.findViewById(R.id.in_match_tablayout);
        mTvTitle = (TextView) mViewTitle.findViewById(R.id.tv_tablayout_title);
        mTvTitle.setText("搭配");
        mTabLayout = (TabLayout) view.findViewById(R.id.tab_match_tablayout);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_match_content);
    }
}
